package com.example.ex5_springboot.controller;

import com.example.ex5_springboot.Result;
import com.example.ex5_springboot.pojo.AdminLogin;
import com.example.ex5_springboot.services.AdminLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;




@RestController
@CrossOrigin(allowedHeaders = "*")
public class AdminLoginController {
    @Autowired
    private AdminLoginService adminLoginService;

    @PostMapping("/loginAdmin")
    public Result login(@RequestParam("userId")String id,
                        @RequestParam("password")String pwd){

        Integer adminId = 0;
        try {
            adminId = Integer.parseInt(id);
        } catch (NumberFormatException e){
            return Result.fail();
        } catch (Exception e) {
            return Result.fail();
        }

        AdminLogin adminLogin = adminLoginService.getAdminInfoById(adminId);
        if (adminLogin == null){
            return Result.fail();
        } else if (adminLogin.getAdmin_pwd().equals(pwd)){
            return Result.success(adminLogin);
        } else {
            return Result.fail();
        }
    }
}
